package com.hz.userservice.controller;

import com.hz.apiservice.model.request.ApiResponse;
import com.hz.userservice.service.UserManagerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserLoginController {
    @Resource
    UserManagerService userManagerService;
    @PostMapping("/login")
    public String login(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("userType") Integer userType){
        String result=userManagerService.login(username,password,userType);
        return result!=null? ApiResponse.ok(result):ApiResponse.intError();
    }
    @PostMapping("/logout")
    public String logout(@RequestParam("username") String username){
        int result= userManagerService.logout(username);
        return result>0?ApiResponse.ok(result):ApiResponse.intError();
    }

    @PostMapping("/change-pwd")
    public String changePwd(@RequestParam("userId") Integer userId,@RequestParam("originPwd") String originPwd,@RequestParam("newPwd") String newPwd,@RequestParam("newPwd2") String newPwd2){
        if (!newPwd.equals(newPwd2)){
            return ApiResponse.intError();
        }
        int result = userManagerService.changePwd(userId,originPwd,newPwd);
        return result>0?ApiResponse.ok(result):ApiResponse.intError();
    }
}
